$(function(){
	
	//检查订单总金额
	checkOrderAmount();
	
	$('.order-type .select').click(function(){
		
		var obj = $(this);
		var items = obj.parents('.order-items').find('.product-items').find('.select');
		var btnObj = obj.parents('.order-items').find('.btn-submit');
		var check = '/mobile/assets/images/check.png';
		var uncheck = '/mobile/assets/images/uncheck.png';
		var status = $(this).attr('src');
		var disabledClass='btn-disabled';
		
		if ( status == check ){
			obj.attr('src',uncheck);
			items.attr('src',uncheck);
			btnObj.attr('disabled',true).addClass(disabledClass);
		} else {
			obj.attr('src',check);
			items.attr('src',check);
		}
		checkOrderAmount();
	});
	
	$('.product-items .select').click(function(){
		
		var obj = $(this);
		var goodsObj = obj.parent().find('.goodsitem');
		var parentObj =  obj.parents('.order-items').find('.order-type .select');
		var btnObj = obj.parents('.order-items').find('.btn-submit');
		var itemsObj = obj.parents('.order-items').find('.product-items .select');
		var check = '/mobile/assets/images/check.png';
		var uncheck = '/mobile/assets/images/uncheck.png';
		var status = $(this).attr('src');
		var itemsStatus = uncheck;		
		
		if ( status == check ){
			obj.attr('src',uncheck);
			goodsObj.attr('checked',false);
		} else {
			obj.attr('src',check);
			goodsObj.attr('checked',true);
		}
		for( var x = 0 ; x < itemsObj.size() ; x++){
			var items = itemsObj.eq(x);
			if(items.attr('src') == check){
				itemsStatus  = check;
				
			}
		}
		parentObj.attr('src',itemsStatus);
		checkOrderAmount();
	});
	
	//操作按钮点击效果
	$('.order-items .op-num').click(function(){
		
		var obj = $(this);
		var numObj = obj.parent('.product-number').find('.num');
		var priceObj = obj.parents('.product-items').find('.product-price');
		var selectObj = obj.parents('.product-items').find('.select');
		var orderObj = obj.parents('.order-items');
		var btnObj = obj.parents('.order-items').find('.btn-submit');
		var num = parseInt(numObj.text());
		var recId = parseInt(obj.parents('.product-items').find('.product-param-rec-id').text());
		var goodsId = parseInt(obj.parents('.product-items').find('.product-param-goods-id').text());
		var psStyle = parseInt(obj.parents('.product-items').find('.product-param-ps-style').text());
		
		if( !btnObj.hasClass('btn-disabled') || ( btnObj.hasClass('btn-disabled') && obj.hasClass('mul')) ){
			
			selectObj.attr('src','/mobile/assets/images/check.png');
			
			if(obj.hasClass('add')){
				//表示进行的是加法操作
				num++;
			}else{
				num--;
				if( num < 0 ){
					num = 0;	
				}
			}
			
			$.ajax({
				type:"get",
				url:"cart.php?act=update_group_cart&rec_id="+recId+"&number="+num+"&goods_id="+goodsId+"&ps_style="+psStyle,
				async : false,
				dataType:"json",
				success : function(data){
					if(data.status ==0 ){
						//表示数据更新成功
						numObj.text(num);	//更新数量
						priceObj.text(data.subtotal); //更新价格
						updateCartTotalInfo(orderObj);
					}else{
						showErrors(data.msg , '确定');
					}
					checkOrderAmount();
					updateCartNum();
				}
			});	
		}
		
	});
		
});
	
//更新购物车合计信息
function updateCartTotalInfo($obj){
	
	var goodsItems = $obj.find('.product-items').find('.select');
	var btnObj = $obj.find('.btn-submit');
	var priceObj = $obj.find('.product-total-price');
	var rateObj = $obj.find('.order-goods-rate');
	var totalObj = $obj.find('.order-total-price');
	var price = 0;
	var num = 0;
	var rate = 0;
	var total = 0;
	var check = '/mobile/assets/images/check.png';
	var ps_style = $obj.find('.product-info').find('.order-ps-style').val();
        var temp = 0;
        var temp_c = 0;
	goodsItems.each(function(){
		if( $(this).attr('src') == check){
			var goodsObj = $(this).parent('.product-items');
			var goodsNum = parseInt(goodsObj.find('.num').text());
			var goodsPrice = parseFloat(( goodsObj.find('.product-price').text()).replace(/[^\d.]/g,''));
			var goodsRate = parseFloat(goodsObj.find('.product-param-rate').text());
			num += goodsNum;
			price += goodsPrice;
			rate += goodsPrice * goodsRate;
		}
	});
	
	// total = rate <= 50 ? price : price + rate;
	total = price + rate;
	btnObj.val( '结算（' + num + '件）' );
	
	var priceHtml = price == 0 ? '￥0' : '￥' + price.toFixed(2);
	var rateHtml = rate == 0 ? '￥0' : '￥' + rate.toFixed(2);
	var totalHtml = total == 0 ? '￥0' : '￥' + total.toFixed(2);

	// if( rate <= 50){
	// 	rateHtml = '<i style="text-decoration: line-through; color:#ccc;font-style: normal;"><a href="#" style="color:#666;" onclick="new Function(taxtips.innerHTML)();">'+rateHtml+'</a></i><i class="iconfont warningtips">&#xe672;</i>';
	// }
        //Fern 2016-11-01 凑单提醒
        var coudan_coupon = $('#coudan_coupon' + ps_style);
        var temp = parseInt(coudan_coupon.find('#lower_amount').text() - price);
        if (temp > 0)
        {
            coudan_coupon.show();
            coudan_coupon.find('em').html(temp);
        } else {
            coudan_coupon.hide();
        }
        //包邮凑单提示
        var coudan_ship = $('#coudan' + ps_style);
        var temp_c = parseInt(coudan_ship.find('#lower').text() - price);
        if (temp_c > 0)
        {
            coudan_ship.show();
            coudan_ship.find('em').html(temp_c);
        } else {
            coudan_ship.hide();
        }
        
	priceObj.html(priceHtml);
	rateObj.html(rateHtml);
	totalObj.html(totalHtml);
}

//检查订单总金额
function checkOrderAmount(){
	
	$('.order-total-price').each(function(){

		var obj = $(this);
		var btnObj = obj.parents('.product-info').find('.btn-submit');
		var orderObj = obj.parents('.order-items');
		var psStyle = parseInt(obj.parents('.order-items').find('.order-ps-style').val()); //配送方式
		var maxAmount = 2000 ;
		var msg = '海关规定，保税备货和保税集货类单个订单总额不能超过'+maxAmount+'元！';
		var disabledClass='btn-disabled';
		updateCartTotalInfo(orderObj);
		var total = parseFloat(obj.text().replace(/[^\d.]/g,''));

		if( psStyle != 3){
			//不是直邮模式订单，需要检查订单总金额
			if( total > maxAmount || total == 0){
				if(orderObj.find('.order-warning').length == 0 ){
					$('<div class="order-warning"><i class="iconfont" style="font-size:1em;color:#07f;">&#xe672;</i>'+msg+'</div>').appendTo(orderObj);
				}
				btnObj.addClass(disabledClass).attr('disabled',true);
			}else{
				orderObj.find('.order-warning').remove();
				btnObj.removeClass(disabledClass).attr('disabled',false);
			}
			
			if( total == 0 ){
				orderObj.find('.order-warning').remove();
			}
		}else{
			// 海外直邮订单处理
			if( total>0 ){
				btnObj.removeClass(disabledClass).attr('disabled',false);
			}else{
				btnObj.addClass(disabledClass).attr('disabled',true);
			}
			
		}

	});
	
}